package cn.icare.cms.aop;

import cn.icare.cms.JwtUtil;
import cn.icare.cms.StringUtil;
import cn.icare.cms.TokenUtil;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import cn.icare.cms.model.vo.rest.UserLoginRespVO;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class UserLogAspect {
//    @Resource
//    private UserLogService userLogService;
    private String userLogDesc;
    private final Logger logger = LoggerFactory.getLogger(UserLogAspect.class);

    @Pointcut(value = "@annotation(cn.icare.cms.aop.UserLog)")
    public void saveUserLog() {
    }

    @Around("@annotation(userLog)")
    public Object doAround(ProceedingJoinPoint pjp, UserLog userLog) {
        this.userLogDesc = userLog.desc();
        try {
            return pjp.proceed();
        } catch (Throwable throwable) {
            this.logger.error("doAround 异常", throwable);
            return null;
        }
    }

    @AfterReturning(returning = "ret", pointcut = "saveUserLog()")
    public void doAfterReturning(Object ret) {
        try {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
            ResponseEntity<RespMsgVO> responseEntity = (ResponseEntity<RespMsgVO>) ret;
            //记录操作日志
//            VUserLogVO vUserLogVO = new VUserLogVO();
//            if (httpServletRequest.getRequestURI().equals("/rest/user/login")) {
//                RespMsgVO respMsgVO = responseEntity.getBody();
//                UserLoginRespVO userLoginRespVO = (UserLoginRespVO) respMsgVO.getResData();
//                if (userLoginRespVO != null && userLoginRespVO.getUser() != null) {
//                    vUserLogVO.setUserid(userLoginRespVO.getUser().getId());
//                }
//            } else {
//                TokenUtil tokenUtil = new TokenUtil();
//                String token = tokenUtil.getToken(httpServletRequest);
//                if (StringUtil.isNotEmpty(token)) {
//                    JwtUtil jwtUtil = new JwtUtil();
//                    vUserLogVO.setUserid(jwtUtil.parseJWT(token));
//                }
//            }
//            vUserLogVO.setResult(responseEntity.getStatusCodeValue());
//            vUserLogVO.setIp(httpServletRequest.getRemoteAddr());
//            vUserLogVO.setContent(this.userLogDesc);
//            this.userLogService.createOne(vUserLogVO);
        } catch (Exception e) {
            this.logger.error("doAfterReturning 异常", e);
        }
    }
}
